package com.kiloai.digcust.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.david.recyclerview.adapter.base.callback.OnViewClickListener;
import com.kiloai.base.basemvp.MVPBaseActivity;
import com.kiloai.base.custom.CompatTitleBar;
import com.kiloai.base.custom.CustomSwipeRefreshLayout;
import com.kiloai.base.custom.HeaderViewRecyclerAdapter;
import com.kiloai.base.entity.TalkList;
import com.kiloai.digcust.R;
import com.kiloai.digcust.adapter.TalkCategoriesAdapter;
import com.kiloai.digcust.mvp.presenter.MainPresenter;


import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends MVPBaseActivity<MainActivity, MainPresenter> {

    @BindView(R.id.compatTitleBar)
    CompatTitleBar compatTitleBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    CustomSwipeRefreshLayout swipeRefreshLayout;
    private MainPresenter presenter;
    private TalkCategoriesAdapter adapter;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;


    @Override
    protected MainPresenter createPresenter() {
        if (presenter == null) {
            presenter = new MainPresenter();
        }
        return presenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        compatTitleBar.setCenterTitle(getString(R.string.app_name));
        adapter = new TalkCategoriesAdapter();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(adapter);
        recyclerView.setAdapter(headerViewRecyclerAdapter);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getTalkList(adapter);

            }
        });
        adapter.setOnViewClickListener(new OnViewClickListener<TalkList>() {
            @Override
            public void onViewClick(View view, int pos, TalkList bean) {
                Intent intent = new Intent(MainActivity.this,TalkListActivity.class);
                intent.putExtra("talkId",bean.getTitle());
                startActivity(intent);
            }
        });
        firstInitData();
    }

    private void firstInitData() {
        //首次进入动画
      /*  swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });*/
        presenter.getTalkList(adapter);
    }

    /**
     * 数据请求完成
     */
    public void loadedSuccess() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.loadingOver();
        }
    }

}
